<?php
namespace app\api\service;
/**
 * Token Service
 * User: zng
 * Date: 2019/10/24
 * Time: 16:25
 */
use Firebase\JWT\JWT;
use Firebase\JWT\SignatureInvalidException;

class Token {

    public static function get( array $userInfo=[] )
    {
        $key = config('jwt.key');
        $token = [
            'iss' => '',
            'aud' => '',
            'iat' => time(),
            'nbf' => time(),
            'userInfo' => $userInfo
        ];
        return JWT::encode($token, $key, 'HS256');
    }

    public static function decode( string $token )
    {
        $key = config('jwt.key');
        try{
            return returnArray((array)JWT::decode($token, $key, ['HS256']));
        }catch (SignatureInvalidException $e) {
            return returnArray([], '签名验证失败','422');
        }
    }

}